package com.kolon.business.mResourceRes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonNews.controller.MResourceResListController")
public class MResourceResListController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	@RequestMapping(value="/business/mResourceRes/mResourceResList.do")
	public ModelAndView controller(HttpServletRequest request,
			
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String companyCD = (String)userMap.get("CompanyCD");
		// 그룹자원
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		buffer.append("/resource_res.aspx?mode=").append("1");
		buffer.append("&strCompanyCD=").append(companyCD);
		buffer.append("&type=").append("GROUP");
		buffer.append("&catid=1");
		buffer.append("&userid=").append(id);
		buffer.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren();
		Iterator itXml = xmlList.iterator();
		List mapList = new ArrayList();
		Map map = new HashMap();
		// XML 데이터 
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("ResourceCode", el.getChild("ResourceCode").getText());
			map.put("ResourceNM", el.getChild("ResourceNM").getText());
			map.put("ResourceNMUtf", ComUtil.getEncode(el.getChild("ResourceNM").getText(),"UTF-8"));
			map.put("ResourceParentID", el.getChild("ResourceParentID").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("ResourceOrder", el.getChild("ResourceOrder").getText());
			map.put("ResourceType", el.getChild("ResourceType").getText());
			map.put("ResourceOption", el.getChild("ResourceOption").getText());
			map.put("Depth", el.getChild("Depth").getText());
			map.put("SubCount", el.getChild("SubCount").getText());
			map.put("IsSubDepth", el.getChild("IsSubDepth").getText());
			map.put("nSuperUser", el.getChild("nSuperUser").getText());
			map.put("nResourceFolderAdmin", el.getChild("nResourceFolderAdmin").getText());
			map.put("DownYN", el.getChild("DownYN").getText());
			mapList.add(map);
		}
		// 회사자원
		StringBuffer buffer2 = new StringBuffer();
		buffer2.append(url);
		buffer2.append("/resource_res.aspx?mode=").append("1");
		buffer2.append("&strCompanyCD=").append(companyCD);
		buffer2.append("&type=").append("COMPANY");
		buffer2.append("&catid=2");
		buffer2.append("&userid=").append(id);
		buffer2.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc2 = ComUtil.getDocument(buffer2.toString(), id, pw);
		Element element2 = doc2.getRootElement();
		List xmlList2 = element2.getChildren();
		Iterator itXml2 = xmlList2.iterator();
		List mapList2 = new ArrayList();
		// XML 데이터 
		while (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map = new HashMap();
			map.put("ResourceCode", el.getChild("ResourceCode").getText());
			map.put("ResourceNM", el.getChild("ResourceNM").getText());
			map.put("ResourceNMUtf", ComUtil.getEncode(el.getChild("ResourceNM").getText(),"UTF-8"));
			map.put("ResourceParentID", el.getChild("ResourceParentID").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("ResourceOrder", el.getChild("ResourceOrder").getText());
			map.put("ResourceType", el.getChild("ResourceType").getText());
			map.put("ResourceOption", ComUtil.getEncode(el.getChild("ResourceOption").getText(),"UTF-8"));
			map.put("Depth", el.getChild("Depth").getText());
			map.put("SubCount", el.getChild("SubCount").getText());
			map.put("IsSubDepth", el.getChild("IsSubDepth").getText());	
			map.put("nSuperUser", el.getChild("nSuperUser").getText());
			map.put("nResourceFolderAdmin", el.getChild("nResourceFolderAdmin").getText());
			map.put("DownYN", el.getChild("DownYN").getText());
			mapList2.add(map);
		}
		
		// 부서자원
		StringBuffer buffer3 = new StringBuffer();
		buffer3.append(url);
		buffer3.append("/resource_res.aspx?mode=").append("1");
		buffer3.append("&strCompanyCD=").append(companyCD);
		buffer3.append("&type=").append("DEPT");
		buffer3.append("&catid=3");
		buffer3.append("&userid=").append(id);
		buffer3.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc3 = ComUtil.getDocument(buffer3.toString(), id, pw);
		Element element3 = doc3.getRootElement();
		List xmlList3 = element3.getChildren();
		Iterator itXml3 = xmlList3.iterator();
		List mapList3 = new ArrayList();
		// XML 데이터 
		while (itXml3.hasNext()) {
			Element el = (Element)itXml3.next();
			map = new HashMap();
			map.put("ResourceCode", el.getChild("ResourceCode").getText());
			map.put("ResourceNM", el.getChild("ResourceNM").getText());
			map.put("ResourceNMUtf", ComUtil.getEncode(el.getChild("ResourceNM").getText(),"UTF-8"));
			map.put("ResourceParentID", el.getChild("ResourceParentID").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("ResourceOrder", el.getChild("ResourceOrder").getText());
			map.put("ResourceType", el.getChild("ResourceType").getText());
			map.put("ResourceOption", ComUtil.getEncode(el.getChild("ResourceOption").getText(),"UTF-8"));
			map.put("Depth", el.getChild("Depth").getText());
			map.put("SubCount", el.getChild("SubCount").getText());
			map.put("IsSubDepth", el.getChild("IsSubDepth").getText());	
			map.put("nSuperUser", el.getChild("nSuperUser").getText());
			map.put("nResourceFolderAdmin", el.getChild("nResourceFolderAdmin").getText());
			map.put("DownYN", el.getChild("DownYN").getText());
			mapList3.add(map);
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("mapList", mapList);
		modelAndView.addObject("mapList2", mapList2);
		modelAndView.addObject("mapList3", mapList3);
		modelAndView.addObject("type", request.getParameter("type"));
		modelAndView.addObject("catid", request.getParameter("catid"));		
		//등록 폼에서 회의실 찾기 클릭시(리스트->등록폼 이동)
		modelAndView.addObject("ResType", request.getParameter("ResType"));
		String step="";
		if("GROUP".equals(request.getParameter("type"))) {
			step = "그룹자원";
		}else if("COMPANY".equals(request.getParameter("type"))) {
			step = "회사자원";
		}else if("DEPT".equals(request.getParameter("type"))) {
			step = "부서자원";
		}
		modelAndView.addObject("strCompanyCD", companyCD);
		return modelAndView;
	}

}